/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2020 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

#ifndef OSGEARTH_REGISTRY
#define OSGEARTH_REGISTRY 1

#include <osgEarth/Common>
#include <osgEarth/CachePolicy>
#include <osgEarth/ShaderGenerator>
#include <osgEarth/Threading>
#include <osgEarth/SpatialReference>
#include <atomic>
#include <set>
#include <unordered_set>
#include <unordered_map>

#define GDAL_SCOPED_LOCK \
    std::lock_guard<osgEarth::Threading::RecursiveMutex> \
        _oe_gdal_lock( osgEarth::getGDALMutex() )

namespace osgText {
    class Font;
}

namespace osgEarth
{
    class Cache;
    class Capabilities;
    class Profile;
    class URIReadCallback;
    class ObjectIndex;
    class Units;
    class StateSetCache;

    namespace Util
    {
        class ShaderFactory;
    }


    //! Global mutex used to serialize access to GDAL/OGR/PROJ functionality
    extern OSGEARTH_EXPORT Threading::RecursiveMutex& getGDALMutex();

    /**
     * Application-wide global repository.
     */
    class OSGEARTH_EXPORT Registry : public osg::Referenced
    {
    public:
        /** Access the global Registry singleton. Pass in true to destroy it and create a new one. */
        static Registry* instance(bool reset = false);

        /** Gets a well-known named profile instance.
        * @deprecated Use Profile::get(name) */
        const Profile* getNamedProfile( const std::string& name ) const;

        /** Gets the global-geodetic builtin profile
        * @deprecated Use Profile::get("global-geodetic") */
        const Profile* getGlobalGeodeticProfile() const;

        /** @deprecated Gets the global-meractor builtin profile (mercator/WGS84 datum)
        * @deprecated Use Profile::get("global-mercator") */
        const Profile* getGlobalMercatorProfile() const;

        /** Gets the spherical-mercator builtin profile (mercator/sphere)
        * @deprecated Use Profile::get("spherical-mercator") */
        const Profile* getSphericalMercatorProfile() const;

        /** Gets a default cache object - based on environment settings - unless setDefaultCache
            was called to override it. */
        Cache* getDefaultCache() const;

        /** Sets a default cache that a Map will use if none other is specified. */
        void setDefaultCache(Cache* cache);

        /** The default cache policy (used when no policy is set elsewhere) */
        const optional<CachePolicy>& defaultCachePolicy() const;
        void setDefaultCachePolicy( const CachePolicy& policy );

        /** The override cache policy (overrides all others if set) */
        const optional<CachePolicy>& overrideCachePolicy() const;
        void setOverrideCachePolicy( const CachePolicy& policy );

        /** The default cache driver. */
        void setDefaultCacheDriverName( const std::string& name );
        const std::string& getDefaultCacheDriverName() const;

        /**
         * Given a CachePolicy, composites in the default and override cache policies
         * as necessary to create an effective CachePolicy. First it will populate
         * any unset properties in "cp" with defaults if they are available. Then it
         * will override any properties in "cp" with overrides that are available.
         */
        bool resolveCachePolicy(optional<CachePolicy>& cp) const;

        /**
         * Whether the given filename is blacklisted
         */
        bool isBlacklisted(const std::string &filename);

        /**
         * Blacklist the given filename
         */
        void blacklist(const std::string &filename);

        /**
         * Gets the number of blacklisted filenames
         */
        unsigned int getNumBlacklistedFilenames();

        /**
         * Clears the blacklist
         */
        void clearBlacklist();

        /**
         * Sets or gets a default system font to use
         */
        void setDefaultFont( osgText::Font* font );
        osgText::Font* getDefaultFont();

        /**
         * The graphics hardware capabilities for this platform.
         */
        bool hasCapabilities() const;
        const Capabilities& getCapabilities() const;
        void setCapabilities( Capabilities* caps );
        static const Capabilities& capabilities() { return instance()->getCapabilities(); }

        /**
         * Gets or sets the default shader factory. You can replace the default
         * shader factory if you want to alter any of osgEarth's baseline shaders
         * (advanced usage).
         */
        Util::ShaderFactory* getShaderFactory() const;
        void setShaderFactory( Util::ShaderFactory* lib );
        static Util::ShaderFactory* shaderFactory() { return instance()->getShaderFactory(); }

        /**
         * The default shader generator.
         */
        ShaderGeneratorProxy getShaderGenerator() const;
        void setShaderGenerator(ShaderGenerator* gen);
        static ShaderGeneratorProxy shaderGenerator() { return instance()->getShaderGenerator(); }

        /**
         * Global object index.
         */
        ObjectIndex* getObjectIndex() const;
        static ObjectIndex* objectIndex() { return instance()->getObjectIndex(); }

        /**
         * A default StateSetCache to use by any process that uses one.
         * A StateSetCache assist in stateset sharing across multiple nodes.
         * Note: A registry-wide SSC is only supported in OSG 3.1.4+. See
         * the Registry.cpp comments for details.
         */
        StateSetCache* getStateSetCache() const;
        void setStateSetCache( StateSetCache* cache );
        static StateSetCache* stateSetCache() { return instance()->getStateSetCache(); }

        /**
         * A shared cache for osg::Program objects created by the shader
         * composition subsystem (VirtualProgram).
         */
        ProgramRepo& getProgramRepo();
        static ProgramRepo& programRepo() { return instance()->getProgramRepo(); }

        /**
         * Generates an instance-wide global unique ID.
         */
        UID createUID();

        /**
         * Sets a global read callback for URI objects.
         */
        void setURIReadCallback( URIReadCallback* callback );

        /**
         * Gets the global read callback for URI objects.
         */
        URIReadCallback* getURIReadCallback() const;

        /**
         * Gets the default set of osgDB::Options to use.
         * @deprecated
         */
        const osgDB::Options* getDefaultOptions() const;

        /**
         * Clones an options structure (fixing the archive caching), or creates
         * a new one.
         */
        static osgDB::Options* cloneOrCreateOptions( const osgDB::Options* options =0L );

        /**
         * Registers a Units definition.
         */
        void registerUnits( const Units* staticInstance );

        const Units* getUnits(const std::string& name) const;

        /**
         * The name of the default terrain engine driver
         */
        //void setDefaultTerrainEngineDriverName( const std::string& name );
        const std::string& getDefaultTerrainEngineDriverName() const { return _terrainEngineDriver; }

        /**
         * If set, all MapNodes will use the terrain driver specified here regardless
         * of the driver in the TerrainOptions/earth file.
         */
        optional<std::string>& overrideTerrainEngineDriverName() { return _overrideTerrainEngineDriverName; }
        const optional<std::string>& overrideTerrainEngineDriverName() const { return _overrideTerrainEngineDriverName; }

        /**
         * For debugging - tracks activities in progress.
         */
        void startActivity(const std::string& name);
        void startActivity(const std::string& name, const std::string& text);
        void endActivity(const std::string& name);
        void getActivities(std::set<std::string>& output);

        /**
         * Gets the mime-type corresponding to a given extension.
         */
        std::string getMimeTypeForExtension(const std::string& extension);

        /**
         * Gets the file extension corresponding to a given mime-type.
         */
        std::string getExtensionForMimeType(const std::string& mimeType);

        /**
         * Sets the policy for calling osg::Texture::setUnRefImageDataAfterApply
         * in the osgEarth terrain engine.
         */
        optional<bool>& unRefImageDataAfterApply() { return _unRefImageDataAfterApply; }
        const optional<bool>& unRefImageDataAfterApply() const { return _unRefImageDataAfterApply; }

        /**
         * Adds a texture image unit number that osgEarth should never use.
         */
        void setTextureImageUnitOffLimits(int unit);
        const std::set<int> getOffLimitsTextureImageUnits() const;

        /**
         * Gets the device pixel ratio.
         */
        float getDevicePixelRatio() const;

        /**
        * Sets the device pixel ratio.  This value will be used to scale the size of objects specified in pixels.
        * This value is useful when running in high dpi environments on high resolution displays.
        */
        void setDevicePixelRatio(float devicePixelRatio);

        /**
         * Maximum number of vertices to allow in a single osg::Geometry drawable
         * when performing optimization processing. This varies based on hardware
         * capabilities. Default is 65535.
         */
        void setMaxNumberOfVertsPerDrawable(unsigned value);
        unsigned getMaxNumberOfVertsPerDrawable() const;

        /**
         * Release OpenGL resources associated with anything in the reigstry
         */
        void releaseGLObjects(class osg::State*) const;

        //! Release any resources held by the registry so it can be re-used
        void release();

        //! Register singleton objects with the registry and defer
        //! their destruction to happen with the registry's own.
        template<typename R>
            R* registerSingleton(R* singleton)
        {
            Threading::ScopedMutexLock lock(_regMutex);
            _singletons.push_back(singleton);
            return singleton;
        }

    protected:
        virtual ~Registry();
        Registry();

        mutable Threading::Mutex _regMutex;

        ProgramRepo _programRepo;

        mutable osg::ref_ptr<Cache>   _defaultCache;
        mutable optional<CachePolicy> _defaultCachePolicy;
        mutable optional<CachePolicy> _overrideCachePolicy;

        mutable bool _overrideCachePolicyInitialized;

        typedef std::unordered_set<std::string> StringSet;
        Threading::Mutexed<StringSet> _blacklist;

        osg::ref_ptr<Util::ShaderFactory> _shaderLib;
        osg::ref_ptr<ShaderGenerator> _shaderGen;

        // unique ID generator:
        std::atomic_int _uidGen;

        // system capabilities:
        osg::ref_ptr< Capabilities > _caps;
        mutable Threading::Mutex     _capsMutex;
        void initCapabilities();

        osg::ref_ptr<osgDB::Options> _defaultOptions;

        osg::ref_ptr<URIReadCallback> _uriReadCallback;

        osg::ref_ptr<osgText::Font> _defaultFont;

        typedef std::vector<const Units*> UnitsVector;
        UnitsVector _unitsVector;
        //mutable Threading::Mutex _unitsVectorMutex;

        osg::ref_ptr<StateSetCache> _stateSetCache;

        std::string _terrainEngineDriver;
        optional<std::string> _overrideTerrainEngineDriverName;

        mutable optional<std::string> _cacheDriver;

        typedef std::pair<std::string,std::string> Activity;
        struct ActivityLess {
            bool operator()(const Activity& lhs, const Activity& rhs) const {
                return lhs.first < rhs.first;
            }
        };
        std::set<Activity,ActivityLess> _activities;
        mutable Threading::Mutex _activityMutex;

        optional<bool> _unRefImageDataAfterApply;

        osg::ref_ptr<ObjectIndex> _objectIndex;

        std::set<int> _offLimitsTextureImageUnits;

        float _devicePixelRatio;

        typedef std::unordered_map<SpatialReference::Key, osg::ref_ptr<SpatialReference> > SRSCache;
        Threading::Mutexed<SRSCache> _srsCache;

        unsigned _maxVertsPerDrawable;
        std::vector<osg::ref_ptr<osg::Referenced>> _singletons;

        //! Gets a spatial reference object (internal)
        //! Please use SpatialReference::get() instead of calling this directly
        osg::ref_ptr<SpatialReference> getOrCreateSRS(const SpatialReference::Key& key);
        friend class SpatialReference;
    };
}


namespace
{
    /** Proxy class for automatic registration of Units with the Registry.*/
    struct osgEarthRegisterUnits {
        osgEarthRegisterUnits(const osgEarth::Units* units) {
            osgEarth::Registry::instance()->registerUnits(units);
        }
    };
}

#define OSGEARTH_REGISTER_UNITS(NAME,INSTANCE) \
    extern "C" void osgearth_units_##NAME(void) {} \
    static osgEarthRegisterUnits s_osgEarthRegistryUnitsProxy##NAME (INSTANCE)


#define USE_OSGEARTH_UNITS(NAME ) \
    extern "C" void osgearth_units_##NAME(void); \
    static osgDB::PluginFunctionProxy proxy_osgearth_units_##NAME(osgearth_units_##NAME);



#endif //OSGEARTH_REGISTRY
